var util = require('../../libs/util.js');
var app = getApp()
Page({
  data: {
    hasData: false,
    userInfo: {},
    truename: "",
    tab: 3
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--平台调度端" });
    var that = this;
    this.loadData();
  },
  loadData: function () {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("usuallyPath", {
      CO_ID: app.globalData.CO_ID
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        that.setData({
          hasData: true
        });
        var _data = res.data.data;
        that.setData({
          pathData: _data
        })
      }
    });
  },
  settingPath: function (e) {
    var that = this;
    var id = e.currentTarget.id, data = that.data.pathData;
    for (var i = 0; i < data.length;i++){
      if(data[i].ID == id){

        wx.redirectTo({
          url: '/pages/schedule/path_setting?id=' + id + '&name=' + data[i].name + '&wholemile=' + (data[i].wholemile == 0 ? data[i].mile : data[i].wholemile) + '&mile=' + data[i].mile + '&subsidy=' + data[i].subsidy
        })
      }
    }
  },
  deletePath: function (e) {
    var id = e.currentTarget.id;
    wx.showModal({
      title: '删除路线',
      content: '是否确认删除本路线',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候...',
          });
          util.putData("deletePath", {
            id: id
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showLoading({
                title: "删除成功",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/schedule/path'
                    })
                  }, 3000);
                }
              });
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  }
})
